<?php
namespace app\my\model;
use app\base\model\BaseModel;
use think\Db;
use think\facade\Log;
/**
 * 
 */
class MyFriendModel extends BaseModel
{

	public function freind_list($openid='',$data=[]){
		if(isset($data['nickname'])){
		if(!empty($data['nickname'])){
				$wherea=[
					['u.Usernickname','like','%'.$data['nickname'].'%'],
					['f.openid2','=',$openid]
				];

				$whereb=[
					['u.Usernickname','like','%'.$data['nickname'].'%'],
					['f.openid1','=',$openid]
				];
			}else{
			$wherea['f.openid2']=$openid;
			$whereb['f.openid1']=$openid;
		}
		
			
		}else{
			$wherea['f.openid2']=$openid;
			$whereb['f.openid1']=$openid;
		}
		
		$pre = ($data['page']-1)*$data['limit'];
		$sql =Db::name('user_friend')->alias('f')->field('u.Userid,u.Openid,u.Usernickname,u.UserImg,u.Openid,u.defaultToken as Token,u.Token as currToken')->join('userinfo u','f.openid1=u.Openid')->where($wherea)->fetchSql(true)->limit($pre,$data['limit'])->select();
		$res = Db::name('user_friend')->alias('f')->field('u.Userid,u.Openid,u.Usernickname,u.UserImg,u.Openid,u.defaultToken as Token,u.Token as currToken')->join('userinfo u','f.openid2=u.Openid')->where($whereb)->union($sql)->limit($pre,$data['limit'])->select();
		return $res;
	}
}